#alexander F. gazmararian
#afg2@princeton.edu
#January 9, 2024

#Load packages
library(gsheet)
library(tidyverse)
library(tidylog)
library(lubridate)
library(gridExtra)

#load text data
g <- readRDS(here("data", "inter", "localnews", "localnews_dataset.rds"))

#plot descriptive trends
#reason
p.time <- g %>%
  mutate(year=lubridate::year(date_ymd)) %>%
  pivot_longer(cols=c(Regulations,Gas,Other,Market)) %>%
  mutate(
    name = case_when(
      name == "Gas" ~ "Gas",
      name == "Regulations" ~ "Regulations",
      name %in% c("Other", "Market") ~ "Other",
      T ~ NA_character_
    )) %>%
  group_by(year,name) %>%
  summarize(n=sum(value,na.rm=TRUE)) %>%
  ggplot(aes(x=year,y=n,lty=name,color=name,shape=name)) +
  geom_line(size=1) +
  geom_point(size=3.5) +
  theme_bw(base_size = 14) +
  labs(title="Local News Coverage over Time",
       x="Year", y="Article Mentions",color="",lty="",shape="") +
  theme(
    panel.grid = element_blank(),
    legend.position = c(.92,.8),
    legend.background = element_rect(fill='transparent'),
    legend.box.background = element_rect(fill='transparent',color="transparent"),
    plot.title = element_text(hjust=.5)
  ) +
  scale_color_grey() +
  scale_y_continuous(expand=c(0,0),limits = c(0,12))
p.mine <- 
  g %>%
  mutate(year=lubridate::year(date_ymd)) %>%
  pivot_longer(cols=c(Regulations,Gas,Other,Market)) %>%
  mutate(
    name = case_when(
      name == "Gas" ~ "Gas",
      name == "Regulations" ~ "Regulations",
      name %in% c("Other", "Market") ~ "Other",
      T ~ NA_character_
    ),
    Mine = ifelse(is.na(Mine), "General Coverage", Mine)
  ) %>%
  group_by(name,Mine) %>%
  summarize(n=sum(value,na.rm=TRUE)) %>%
  mutate(gencov = ifelse(Mine == "General Coverage", "General Coverage", "Specific Mine Closures")) %>%
  ggplot() +
  geom_col(aes(x=Mine,y=n,fill=name),position="dodge") +
  theme_bw(base_size = 14) +
  facet_wrap(~gencov, scales = "free_x") +
  scale_y_continuous(expand = c(0,0), limits = c(0, 25)) +
  scale_x_discrete(
    labels = scales::wrap_format(12)
  ) +
  scale_fill_grey() +
  labs(x="Article Subject", y="Article Mentions",fill="",
       title="Local News Coverage by Mine Closure") +
  theme(
    panel.grid = element_blank(),
    legend.position = c(.1,.82),
    legend.background = element_rect(fill='transparent'),
    legend.box.background = element_rect(fill='transparent',color="transparent"),
    plot.title = element_text(hjust=.5)
  )

#Figure N1
p.out <- grid.arrange(p.time, p.mine)
ggsave(
  p.out,
  filename = here("output", "figures", "si_fig_N1_localnews.png"),
  dpi = 300,
  scale = 1.5,
  height = 5,
  width = 6.5
)
#Descriptive stats mentioned in the appendix
g %>%
  mutate(
    regandgas = case_when(
      Regulations==1&Gas==1~1,
      T ~ 0
    )
  ) %>%
  pivot_longer(cols=c(Regulations,Gas,regandgas)) %>%
  group_by(name) %>%
  summarize(n=sum(value,na.rm=TRUE))

